package cn.xxd.controller;


import cn.xxd.common.UrlUtil;
import cn.xxd.entity.*;
import cn.xxd.service.ContentService;
import cn.xxd.service.ShopCartService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ShopCartController {

    @Reference(version = "1.0.0", timeout = 100000)
    private ShopCartService shopCartService;
    @Reference(version = "1.0.0", timeout = 100000)
    private ContentService contentService;

    @Value("${ORDERURL}")
    private String ORDERURL;

    @Value("${HOMEURL}")
    private String HOMEURL;

    @Value("${SSOURL}")
    private String SSOURL;


    @Value("${SEARCHURL}")
    private String SEARCHURL;

    @Value("${CAERTURL}")
    private String CAERTURL;

    @RequestMapping(value = "/")
    public String show(@RequestParam("id") long id, Model model, HttpServletRequest request) {
        LoginUser user = (LoginUser) request.getAttribute("user");
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        } else {
            return "";
        }
        XxdItem item = contentService.findItemById(id);
        List<XxdItem> homeCartItems = contentService.findHomeCartItem((int) user.getId());
        List<HomeContent> homeContents = contentService.find();
        model.addAttribute("homeCartItems", homeCartItems);
        model.addAttribute("item", item);
        model.addAttribute("ORDERURL", ORDERURL);
        model.addAttribute("HOMEURL", HOMEURL);
        model.addAttribute("SSOURL", SSOURL);
        model.addAttribute("SEARCHURL", SEARCHURL);
        model.addAttribute("CAERTURL", CAERTURL);
        model.addAttribute("homeContent", homeContents);
        return "single-product";
    }

    @RequestMapping(value = "/cart")
    public String showCart(HttpServletRequest request, Model model) {
        double total = 0;
        LoginUser user = (LoginUser) request.getAttribute("user");
        Long userId = user.getId();
        List<XxdItem> items = contentService.findHomeCartItem(userId.intValue());
        for (XxdItem item : items) {
            total += item.getSum();
        }


        String url = UrlUtil.getUrl(request);
        model.addAttribute("items", items);
        model.addAttribute("total", total);
        model.addAttribute("url", url);
        model.addAttribute("ORDERURL", ORDERURL);
        model.addAttribute("HOMEURL", HOMEURL);
        model.addAttribute("SSOURL", SSOURL);
        model.addAttribute("SEARCHURL", SEARCHURL);
        model.addAttribute("CAERTURL", CAERTURL);
        return "cart";
    }

    @PostMapping(value = "/user/cart/remove")
    @ResponseBody
    public ReturnT removeCart(String id, HttpServletRequest request, Model model) {
        int result = shopCartService.removeOrder(id);
        if (result > 0) {
            return new ReturnT(200, "success");
        }
        return new ReturnT(500, "failed");
    }

    @RequestMapping(value = "/addshopcart")
    @ResponseBody
    public String saveOrUpdate(long id, long num, HttpServletRequest request) {
        LoginUser user = (LoginUser) request.getAttribute("user");
        long userId = user.getId();
        ShopCart shopCart = new ShopCart();
        shopCart.setUid(userId);
        shopCart.setItem_id(id);
        shopCart.setNum(num);
        shopCart.setStatus("1");
        shopCart.setCreateTime(new Date());
        shopCart.setUpdateTime(new Date());
        shopCartService.saveOrUpdate(shopCart);
        return "200";
    }
}
